package com.lonely.common.utils.enums;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReflectUtil;
import com.lonely.common.core.domain.Select;

import java.util.ArrayList;
import java.util.List;

/**
 * 枚举转select工具类
 *
 * @author fzzf
 */
public class Enum2SelectsUtil {


    /**
     * 将指定枚举转为select
     *
     * @param enumClass
     * @param labelMethodName
     * @param valueMethodName
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>> List<Select> getSelects(Class<T> enumClass, String labelMethodName, String valueMethodName) {
        List<Select> selects = new ArrayList<>();
        for (T t : enumClass.getEnumConstants()) {
            String label = Convert.toStr(ReflectUtil.invoke(t, labelMethodName));
            String value = Convert.toStr(ReflectUtil.invoke(t, valueMethodName));
            selects.add(new Select().setLabel(label).setValue(value));
        }
        return selects;
    }


}
